﻿using ArtMath.CommonType;
using ArtMath.Flag;
using ArtMath.Resource;
using ArtMath.Setting;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace ArtMath.Window
{
    public partial class WelcomeWindow : DockContent
    {
        LinkLabel[] recentDocumentLinks = new LinkLabel[10];
        public WelcomeWindow()
        {
            InitializeComponent();
            cbShowAfterStart.Checked = Settings.Instance.ShowHomePageAfterStartup;
            cbCloseAfterLoad.Checked = Settings.Instance.CloseHomePageAfterDocumentLoaded;
            int row = 0;
            foreach (Tuple<string, string> item in UserOptions.Instance.RecentDocuments)
            {
                if (row >= 10) break;
                recentDocumentLinks[row] = new LinkLabel()
                {
                    Text = item.Item1,
                    Font = lblNewDocument.Font,
                    LinkColor = Color.DeepSkyBlue,
                    Tag = item,
                };
                recentDocumentLinks[row].LinkClicked += RecentDocument_LinkClicked;
                tableLayoutPanel2.Controls.Add(recentDocumentLinks[row], 0, row);
                row++;
            }
            ChangeColorTheme(Settings.Instance.ColorTheme);
        }
        public void ChangeColorTheme(UIColorTheme theme)
        {
            switch (theme)
            {
                case UIColorTheme.Light:
                    EnableVSRenderer(VisualStudioToolStripExtender.VsVersion.Vs2015, vS2015LightTheme1);
                    BackColor = Color.FromArgb(238, 238, 242);
                    lblSoftName.ForeColor = label1.ForeColor = label2.ForeColor = cbCloseAfterLoad.ForeColor = cbShowAfterStart.ForeColor = Color.Black;
                    lblNewDocument.LinkColor = lblNewDefaultDocument.LinkColor = lblOpenDocument.LinkColor = Color.Blue;
                    foreach (LinkLabel item in recentDocumentLinks)
                    {
                        if (item == null) continue;
                        item.LinkColor = Color.Blue;
                    }
                    break;
                case UIColorTheme.Blue:
                    EnableVSRenderer(VisualStudioToolStripExtender.VsVersion.Vs2015, vS2015BlueTheme1);
                    BackColor = Color.FromArgb(41, 57, 85);
                    lblSoftName.ForeColor = label1.ForeColor = label2.ForeColor = cbShowAfterStart.ForeColor = cbCloseAfterLoad.ForeColor = Color.White;
                    lblNewDocument.LinkColor = lblNewDefaultDocument.LinkColor = lblOpenDocument.LinkColor = Color.DeepSkyBlue;
                    foreach (LinkLabel item in recentDocumentLinks)
                    {
                        if (item == null) continue;
                        item.LinkColor = Color.DeepSkyBlue;
                    }
                    break;
                case UIColorTheme.Dark:
                    EnableVSRenderer(VisualStudioToolStripExtender.VsVersion.Vs2015, vS2015DarkTheme1);
                    BackColor = Color.FromArgb(45, 45, 48);
                    lblSoftName.ForeColor = label1.ForeColor = label2.ForeColor = cbCloseAfterLoad.ForeColor = cbShowAfterStart.ForeColor = Color.White;
                    break;
            }
        }
        void EnableVSRenderer(VisualStudioToolStripExtender.VsVersion version, ThemeBase theme)
        {
            //visualStudioToolStripExtender1.SetStyle(toolStrip2, version, theme);
            //visualStudioToolStripExtender1.SetStyle(cmsList, version, theme);
            //visualStudioToolStripExtender1.SetStyle(cmsItem, version, theme);
        }
        void RecentDocument_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            LinkLabel ll = sender as LinkLabel;
            Tuple<string, string> tup = ll.Tag as Tuple<string, string>;
            if (File.Exists(tup.Item2))
            {
                Program.WinDocuments.OpenDocument(tup.Item2);
            }
            else
            {
                if (MessageBox.Show(string.Format(Resources.Translate("FileNotExist"), tup.Item2) + Resources.FullStop + Resources.Translate("RemoveAsk"), Resources.Translate("Ask"), MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    int index = Array.IndexOf(recentDocumentLinks, ll);
                    recentDocumentLinks[index] = null;
                    tableLayoutPanel2.Controls.Remove(ll);
                    for (int i = index + 1; i < recentDocumentLinks.Length && recentDocumentLinks[i] != null; i++)
                    {
                        //tableLayoutPanel2.Controls.Remove(recentDocumentLinks[i]);
                        tableLayoutPanel2.Controls.Add(recentDocumentLinks[i], 0, i - 1);
                        recentDocumentLinks[i - 1] = recentDocumentLinks[i];
                        recentDocumentLinks[i] = null;
                    }
                    Program.WinMain.RemoveRecentItem(tup);
                    UserOptions.Instance.Remove(tup);
                }
            }
        }
        private void WelcomeWindow_Load(object sender, EventArgs e)
        {
            Program.WinMain.Text = $"{Resources.Translate("HomePage")} - {Flags.SoftwareName}{Flags.AdministratorFlag}";
        }

        private void WelcomeWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Program.WinMain.LinkedDocument == null)
            {
                Program.WinMain.Text = Flags.SoftwareName + Flags.AdministratorFlag;
            }
        }

        private void lblNewDocument_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Program.WinDocuments.AddDocument();
        }

        private void lblOpenDocument_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Program.WinDocuments.OpenDocuments();
        }

        private void lblNewDefaultDocument_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Document doc = Program.WinDocuments.CreateDocument(Resources.Translate("UntitledDocument"));
            SketchPad pad = doc.CreateSketchpad(Resources.Translate("UnnamedSketchpad"), CoordinateSystemType.RectangularCoordinate);
            pad.RegisterPadEvents();
            pad.Activate();
            doc.Modified = false;
        }

        private void cbShowAfterStart_CheckedChanged(object sender, EventArgs e)
        {
            Settings.Instance.ShowHomePageAfterStartup = cbShowAfterStart.Checked;
        }

        private void cbCloseAfterLoad_CheckedChanged(object sender, EventArgs e)
        {
            Settings.Instance.CloseHomePageAfterDocumentLoaded = cbCloseAfterLoad.Checked;
        }
    }
}
